package cn.edu.xhu.library_management.book.controller;

import cn.edu.xhu.library_management.book.client.AreaClient;
import cn.edu.xhu.library_management.book.entity.LmBook;
import cn.edu.xhu.library_management.book.entity.search.LmBookSearch;
import cn.edu.xhu.library_management.book.entity.vo.BookDetail;
import cn.edu.xhu.library_management.book.service.LmBookService;
import cn.edu.xhu.library_management.book.utils.MinioUtils;
import cn.edu.xhu.library_management.common.entity.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: MMJ
 * @Date: 2023 - 03 - 17
 * @Description: 图书模块
 */
@Api(tags = "图书管理模块")
@ApiSort(2)
@RestController
@RequestMapping("/lm_book")
public class BookController {
    @Autowired
    private LmBookService bookService;
    @Autowired
    private MinioUtils minioUtils;

    /* minio 服务器地址 */
    @Value("${minio.server}")
    private String imgServer;
    @Value("${minio.port}")
    private int serverPort;


    @GetMapping("/book/{bookId}")
    @ApiOperation(value = "通过图书Id查询详细信息", produces = "application/json")
    @ApiOperationSupport(order = 211)
    public R findBookDetail(@PathVariable("bookId") Long bookId) {
        BookDetail detail = bookService.findBookDetail(bookId);
        detail.setBoImage(imgServer + ":" + serverPort + detail.getBoImage());
        return R.ok().data("detail", detail);
    }

    @GetMapping("/book")
    @ApiOperation(value = "获取所有图书", produces = "application/json")
    @ApiOperationSupport(order = 212)
    public R findAllBooks() {
        List<LmBook> books = bookService.list();
        books.forEach(book -> book.setBoImage(imgServer + ":" + serverPort + book.getBoImage()));
        return !books.isEmpty() ? R.ok().data("books", books) : R.error("没有数据");
    }

    @GetMapping("/book/category/{pid}")
    @ApiOperation(value = "通过分类查询图书", produces = "application/json")
    @ApiOperationSupport(order = 212)
    public R findBookByCategory(@PathVariable("pid") Integer pid) {
        List<LmBook> books = bookService.findBookByCategory(pid);

        return !books.isEmpty() ? R.ok().data("books", books) : R.error("没有数据");
    }
    @GetMapping("/search")
    @ApiOperation(value = "分页查询", produces = "application/json")
    @ApiOperationSupport(order = 213)
    public R BooksPage(LmBookSearch search) {
        Page<LmBook> books = bookService.search(search);
        books.getRecords().forEach(book -> book.setBoImage(imgServer + ":" + serverPort + book.getBoImage()));
        return R.ok().data("page", books);
    }

    @PostMapping("/book")
    @ApiOperation(value = "添加图书", produces = "application/json")
    @ApiOperationSupport(order = 221)
    public R addBook(@RequestBody LmBook book) {
        boolean flag = bookService.save(book);
        return flag ? R.ok("添加图书成功") : R.error("添加图书失败");
    }

    @PostMapping("/book/upLoadFile")
    @ApiOperation(value = "添加图书图片", produces = "application/json")
    @ApiOperationSupport(order = 222)
    public R addBookImg(MultipartFile file) {
        String url = minioUtils.upload(file);
        return R.ok().data("url", url);
    }

    @PutMapping("/book")
    @ApiOperation(value = "修改图书", produces = "application/json")
    @ApiOperationSupport(order = 231)
    public R updateBook(@RequestBody LmBook book) {
        // 获取图片路径
        String boImage = bookService.getById(book.getId()).getBoImage();
        // 解析图片名称 并删除对应图片
        String oldFileName = boImage.substring(boImage.lastIndexOf("/")+1);

        // 解析上传的文件名
        String newFile = book.getBoImage();
        String newFileName = newFile.substring(newFile.lastIndexOf("/")+1);
        if (!newFileName.equals(oldFileName)) {
            minioUtils.removeFile(oldFileName);
        }

        // 保存到数据库中
        boolean flag = bookService.updateById(book);

        return flag ? R.ok("修改图书信息成功!!!") : R.error("修改区域信息失败!!!");
    }

    @DeleteMapping("/book/{bookId}")
    @ApiOperation(value = "删除图书", produces = "application/json")
    @ApiOperationSupport(order = 241)
    public R deleteBook(@PathVariable("bookId") Integer bookId) {
        // 获取图片路径
        String boImage = bookService.getById(bookId).getBoImage();
        // 解析图片名称 并删除对应图片
        String oldFileName = boImage.substring(boImage.lastIndexOf("/")+1);
        minioUtils.removeFile(oldFileName);
        return bookService.removeById(bookId) ? R.ok("删除图书成功!!!") : R.error("删除图书失败!!!");
    }

}
